package main

import (
	"encoding/json"

	"jlpay-sdk-go/pkg/jlpay/demo"
	"jlpay-sdk-go/pkg/jlpay/models/trans/union"
	serviceunion "jlpay-sdk-go/pkg/jlpay/services/trans/union"
	"jlpay-sdk-go/pkg/jlpay/utils"
)

// 支付类型常量
const (
	PAY_TYPE_WECHAT = "wxpay"    // 微信支付
	PAY_TYPE_ALIPAY = "alipay"   // 支付宝支付
	PAY_TYPE_UNION  = "unionpay" // 银联支付
)

// 构建银联JS支付请求
func buildUnionJsPayRequest() *union.UnionJsPayRequest {
	request := union.NewUnionJsPayRequest()

	// 设置请求参数
	request.SetMchID("849584358120018")
	request.SetTermNo("60131234")
	request.SetPayType(PAY_TYPE_UNION)
	request.SetAppUpIdentifier("UnionPay/1.0")
	request.SetUserAuthCode("gg47vT2aCMw/utQcfQw==")
	request.SetUserID("test_user_001")
	request.SetOutTradeNo("ld201911023465529")
	request.SetPaymentValidTime("10")
	request.SetBody("测试银联行业码支付商品")
	request.SetAttach("测试银联行业码支付描述")
	request.SetTotalFee("1")
	request.SetRemark("测试银联行业码支付备注")
	request.SetNotifyURL("https://help.jlpay.com/api/docs/home")
	request.SetOpUserID("test_user")
	request.SetOpShopID("GD_SZ_001")
	request.SetDeviceInfo("N50007A8890")
	request.SetMchCreateIP("127.0.0.1")
	request.SetLongitude("116.397128")
	request.SetLatitude("39.916527")
	request.SetFrontURL("https://example.com/success")
	request.SetFrontFailURL("https://example.com/fail")
	request.SetQRCode("https://example.com/qr")

	return request
}

// 银联JS支付示例
func unionJsPayDemo(unionJsPayService *serviceunion.UnionJsPayService) {
	// 创建银联JS支付请求
	request := buildUnionJsPayRequest()

	// 序列化请求数据并打印
	requestMap, _ := request.ToMap()
	requestJSON, _ := json.Marshal(requestMap)
	utils.Debugf("银联JS支付请求数据: %s", string(requestJSON))

	// 发送请求
	response, err := unionJsPayService.UnionJsPay(request)
	if err != nil {
		utils.Errorf("银联JS支付请求失败: %v", err)
		return
	}

	// 处理响应 - 使用BaseResponse接口的HandleResponse方法
	response.HandleResponse("银联JS支付")
}

func main() {
	// 设置日志级别为info，显示详细的调试信息
	utils.SetLogLevel("info")

	utils.Info("=============================")
	utils.Info("===== 银联JS支付示例 开始 =====")
	utils.Info("=============================")

	// 创建配置对象和银联JS支付服务
	config := demo.CreateDemoConfig()
	unionJsPayService := serviceunion.NewUnionJsPayService(config)

	unionJsPayDemo(unionJsPayService)

	utils.Info("=============================")
	utils.Info("===== 银联JS支付示例执行完毕 =====")
	utils.Info("=============================")
}
